package com.gouqi.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gouqi.common.BaseResponse;
import com.gouqi.common.DeleteRequest;
import com.gouqi.common.ErrorCode;
import com.gouqi.common.ResultUtils;
import com.gouqi.exception.BusinessException;
import com.gouqi.exception.ThrowUtils;
import com.gouqi.model.dto.userAnswer.UserAnswerAddRequest;
import com.gouqi.model.dto.userAnswer.UserAnswerEditRequest;
import com.gouqi.model.dto.userAnswer.UserAnswerQueryRequest;
import com.gouqi.model.dto.userAnswer.UserAnswerUpdateRequest;
import com.gouqi.model.entity.UserAnswer;
import com.gouqi.model.vo.UserAnswerVO;
import com.gouqi.service.UserAnswerService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户答案接口
 */
@RestController
@RequestMapping("/userAnswer")
@Slf4j
public class UserAnswerController {

    @Resource
    private UserAnswerService userAnswerService;

    // region 增删改查

    @ApiOperation("创建用户答案")
    @PostMapping("/add")
    public BaseResponse<Long> addUserAnswer(@RequestBody UserAnswerAddRequest userAnswerAddRequest, HttpServletRequest request) {
        long newUserAnswerId = userAnswerService.addUserAnswer(userAnswerAddRequest, request);
        return ResultUtils.success(newUserAnswerId);
    }

    @ApiOperation("删除用户答案")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUserAnswer(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        userAnswerService.deleteUserAnswer(deleteRequest, request);
        return ResultUtils.success(true);
    }

    @ApiOperation("更新用户答案（仅管理员可用）")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateUserAnswer(@RequestBody UserAnswerUpdateRequest userAnswerUpdateRequest) {
        userAnswerService.updateUserAnswer(userAnswerUpdateRequest);
        return ResultUtils.success(true);
    }

    @ApiOperation("根据 id 获取用户答案（封装类）")
    @GetMapping("/get/vo")
    public BaseResponse<UserAnswerVO> getUserAnswerVOById(long id, HttpServletRequest request) {
        UserAnswerVO userAnswerVO = userAnswerService.getUserAnswer(id, request);
        return ResultUtils.success(userAnswerVO);
    }

    @ApiOperation("分页获取用户答案列表（仅管理员可用）")
    @PostMapping("/list/page")
    public BaseResponse<Page<UserAnswer>> listUserAnswerByPage(@RequestBody UserAnswerQueryRequest userAnswerQueryRequest) {
        Page<UserAnswer> userAnswerPage = userAnswerService.getUserAnswerListPage(userAnswerQueryRequest);
        return ResultUtils.success(userAnswerPage);
    }

    @ApiOperation("分页获取用户答案列表（封装类）")
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<UserAnswerVO>> listUserAnswerVOByPage(@RequestBody UserAnswerQueryRequest userAnswerQueryRequest,
                                                                   HttpServletRequest request) {
        Page<UserAnswerVO> userAnswerVOPage = userAnswerService.getUserAnswerPage(userAnswerQueryRequest, request);
        return ResultUtils.success(userAnswerVOPage);
    }

    @ApiOperation("分页获取当前登录用户创建的用户答案列表")
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<UserAnswerVO>> listMyUserAnswerVOByPage(@RequestBody UserAnswerQueryRequest userAnswerQueryRequest,
                                                                     HttpServletRequest request) {
        Page<UserAnswerVO> userAnswerVOPage = userAnswerService.getMyUserAnswerVOPage(userAnswerQueryRequest, request);
        return ResultUtils.success(userAnswerVOPage);
    }

    @ApiOperation("编辑用户答案（给用户使用）")
    @PostMapping("/edit")
    public BaseResponse<Boolean> editUserAnswer(@RequestBody UserAnswerEditRequest userAnswerEditRequest, HttpServletRequest request) {
        userAnswerService.editUserAnswer(userAnswerEditRequest, request);
        return ResultUtils.success(true);
    }

    // endregion
    @GetMapping("/generate/id")
    public BaseResponse<Long> generateUserAnswerId() {
        return ResultUtils.success(IdUtil.getSnowflakeNextId());
    }

}
